import webapp2
from lib.toolbox import Sessions
from lib.toolbox import Render


class DisclaimerPage(Render):

    def get(self, url):
        
      self.render("disclaimer.base.html")

    def post(self, url):

        if (url == 'logout'):

            self.response.headers.add_header('Set-Cookie', 'uid=''; Path=/')
            self.redirect("/") 

        elif (url == '/login'):

            s = Sessions()

            username = self.request.get('username')
            password = self.request.get('password')

            uid = s.Login(username, password)

            if uid:

                refer = self.request.get('ref')
                refer = refer if refer else "/"
                
                self.response.headers.add_header('Set-Cookie', 'uid='+ uid +'; Path=/')
                self.redirect(refer)

            else:
                self.render("loginpage.html", username = username, password = password, error="Invalid Login")